﻿using DBContext;
using EV.EventBus;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using ZoneLine.Models.PackageModel;
using ZoneLine.ViewModels;

namespace ZoneLine.Views
{
    /// <summary>
    /// AuthView.xaml 的交互逻辑
    /// </summary>
    public partial class ContinueBoxUpView : Window
    {
        IModelDAL _dal;
        public ContinueBoxUpView(IModelDAL modelDAL, Action<string> act)
        {
            InitializeComponent();
            _act = act;
            _dal = modelDAL;
            MessageCenter.Subscribe(nameof(ContinueBoxUpView), Guid.NewGuid().ToString(), (s) =>
            {
                if (s.Source.Payload is string ss)
                {
                    App.Current.Dispatcher.Invoke(() =>
                    {
                        code.Text = ss;
                    });
                }
                return Task.CompletedTask;
            });
        }

        private Action<string> _act;

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            var res = HandyControl.Controls.MessageBox.Ask("确认需要重新装箱吗？","温馨提示");
            if (res == MessageBoxResult.OK)
            {
                _act.Invoke(code.Text);
                Close();
            }
        }

        private void Button_SelectClick(object sender, RoutedEventArgs e)
        {
            var list = _dal.GetModelList<PackageResult>(o => o.BoxCode == code.Text);
            if (list != null && list.Count > 0)
            {
                var packageTempModels = new List<PackageTempModel>();
                foreach (var item in list)
                {
                    var packageTempModel = new PackageTempModel()
                    {
                        AbsCode = item.AbsCode,
                        Code = item.Code,
                        Brand = item.Brand,
                        LeftRight = item.LeftRight,
                        PrintTime = item.PrintTime,
                        BoxCode = item.BoxCode,
                        PackageTime = item.PackageTime,
                    };
                    packageTempModels.Add(packageTempModel);
                }
                dataGrid.ItemsSource = packageTempModels;
            }
        }
    }
}
